package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UnThreadSafeServlet extends HttpServlet{
	
	private static final long serialVersionUID = 1L; 
    
    private Set messageSet = new HashSet();
  
    public UnThreadSafeServlet() {  
        super();  
    }  
  
    protected void doGet(HttpServletRequest request,  
            HttpServletResponse response) throws ServletException, IOException {  
        this.doPost(request, response);  
    }  
  
    protected void doPost(HttpServletRequest request,  
            HttpServletResponse response) throws ServletException, IOException {
    	messageSet.add(request.getParameter("message"));
//        message = request.getParameter("message");  
        PrintWriter printWriter = response.getWriter();  
//        try {  
//            Thread.sleep(5000);  
//        } catch (InterruptedException e) {  
//            // TODO Auto-generated catch block  
//            e.printStackTrace();  
//        }  
        StringBuilder sb = new StringBuilder();
        Iterator<String> it = messageSet.iterator();  
        while (it.hasNext()) {
          sb.append(it.next());
        }  
        
        printWriter.write(sb.toString());
        
    }  

}